package org.mtmi.ui.controls.edits;

import java.util.Collection;

import org.mtmi.ui.IMG;
import org.mtmi.ui.M;
import org.mtmi.ui.actions.Action;
import org.mtmi.ui.actions.IRetargetableAction;
import org.mtmi.ui.controls.AbstractRangeValueControl;
import org.mtmi.ui.controls.listeners.PopupListener;
import org.mtmi.ui.controls.popups.AbstractButtonPopup;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.scenes.SceneManager;

public class ChangeRangeValueAction extends Action implements IRetargetableAction
{
	private Object target;
	
	public ChangeRangeValueAction() {
		super( PUSH );
	}
	
	@Override
	/** @return true if object is an instance of Keyboard or a collection of keyboard instances */
	public boolean isTargetHandled( Object c ) {
		if( c instanceof AbstractRangeValueControl ) return true;
		if( c instanceof Collection ) {
			for( Object o : ((Collection<?>)c) ) {
				if( !(o instanceof AbstractRangeValueControl ) ) return false;
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void setTarget( Object c ) {
		target = c;
	}
	
	@Override
	public Object getTarget() {
		return target;
	}

	@Override
	public IDrawable getImage() {
		return IMG.GetD( IMG.I_RANGE_VALUE );
	}

	@Override
	public boolean isEnabled() {
		if( target instanceof AbstractRangeValueControl ) {
			return true;
		} else {
			Collection<?> c = (Collection<?>)target;
			for( Object o : c ) {
				if( !( o instanceof AbstractRangeValueControl )) { return false ; }
			}
			return c.size()>0;
		}
	}
	
	private int getMin() {
		if( target instanceof AbstractRangeValueControl ) {
			return ((AbstractRangeValueControl)target).getMinValue();
		} else {
			int min=-1;
			boolean f=true;
			for( Object o : (Collection<?>)target ) {
				AbstractRangeValueControl rvc = (AbstractRangeValueControl)o;
				if( f ) {
					min = rvc.getMinValue();
					f=false;
				} else {
					if( min != rvc.getMinValue() ) {
						min=-1;
						break;
					}
				}
			}
			return min;
		}
	}
	private int getMax() {
		if( target instanceof AbstractRangeValueControl ) {
			return ((AbstractRangeValueControl)target).getMaxValue();
		} else {
			int max=-1;
			boolean f=true;
			for( Object o : (Collection<?>)target ) {
				AbstractRangeValueControl rvc = (AbstractRangeValueControl)o;
				if( f ) {
					max = rvc.getMaxValue();
					f=false;
				} else {
					if( max != rvc.getMaxValue() ) {
						max=-1;
						break;
					}
				}
			}
			return max;
		}
	}
	
	@Override
	public void run() {
		final MinMaxPopup p = new MinMaxPopup( SceneManager.getDefault().getScene(), "Choose Range:", M.SCENE_EDITOR_CONTROL );
		int min = getMin();
		int max = getMax();
		if( min >=0 && max >= 0 ) {
			p.setRange( min, max );
		}
		p.addResponseListener( new PopupListener() {

			@Override
			public void popupButtonSelected(int id) {
				if( id == AbstractButtonPopup.OK ) {
					int min = p.getMin();
					int max = p.getMax();
					if( target instanceof AbstractRangeValueControl ) {
						run( (AbstractRangeValueControl)target, min, max );
					} else {
						for( Object o : (Collection<?>)target ) {
							run( (AbstractRangeValueControl)o, min, max );
						}
					}
				}
			}
		});
		
	}
	
	protected void run( AbstractRangeValueControl rvc, int min, int max ) {
		rvc.setRange( min, max );
	}

}